<?php
    session_start();

require_once 'includes/includes.php';

if(!isset($_SESSION['counter'])) {
        $_SESSION['counter'] = 1;
    }
    
    $counter =& $_SESSION['counter'];
    $counter++;
    
    $defaultCount = Picture::getCounterValue();


$get_album = isset($_GET['album']) ? utf8_urldecode($_GET['album']) : null;
$get_photoId = isset($_GET['image']) ? $_GET['image'] : null;

$albums = Picture::getAlbums();
$countPics = Picture::countPicsInAlbum($get_album);
$photo = Picture::currentPhoto($get_album, $get_photoId);
$has_prev = Picture::hasPrevious($photo);
$has_next = Picture::hasNext($photo);

if($countPics > 0) {
    
    if(!isset($_SESSION['albumName'])) {
        $_SESSION['albumName'] = '';
    }
    
    if(isset($_SESSION['albumName']) && $_SESSION['albumName'] != $get_album) {
        $_SESSION['albumName'] = $get_album;
        $_SESSION['imgCount'] = $countPics;
        $_SESSION['currImgNumber'] = 1;
        
        $imgCount = $_SESSION['imgCount'];
    }
    
    $currImgNumber =& $_SESSION['currImgNumber'];
    $currImgNumber++;
    
}

if($get_album != null) {
    $album_id = Picture::getAlbumId($get_album);
    $albumContent = Picture::getAlbumContent($album_id);
}
else {
    $albumContent = null;
}

$smarty->assign('get_album', $get_album);
$smarty->assign('albums', $albums);

$smarty->assign('has_prev', $has_prev);
$smarty->assign('has_next', $has_next);

$smarty->assign('curPageURL', curPageURL());
$smarty->assign('photo', $photo);
$smarty->assign('albumContent', $albumContent);

$smarty->assign('counter', json_encode($counter));
$smarty->assign('defaultCount', json_encode($defaultCount));


$smarty->display('header.tpl');
$smarty->display('content.tpl');
$smarty->display('footer.tpl');